package com.xyauto.theme.activity

import android.os.Bundle
import android.text.TextUtils
import android.transition.TransitionManager
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import com.xyauto.theme.Constant
import com.xyauto.theme.R
import com.xyauto.theme.base.BaseActivity
import com.xyauto.theme.base.BaseFragment
import com.xyauto.theme.fragment.MainListFragment
import com.xyauto.theme.utils.startLocalTheme

class MainActivity : BaseActivity() {
    companion object {
        private const val TAG = "ASSSSSSSS"
        private const val TAB_PRIVATE = Constant.REQUEST_PRIVATE_TYPE
        private const val TAB_PUBLIC = Constant.REQUEST_PUBLIC_TYPE
    }

    override fun getLayoutId(): Int = R.layout.activity_main

    private val fragments: MutableList<Pair<String, BaseFragment>> by lazy {
        arrayListOf(
            TAB_PUBLIC to MainListFragment.newInstance(TAB_PUBLIC),
            TAB_PRIVATE to MainListFragment.newInstance(TAB_PRIVATE)
        )
    }

    private lateinit var mTabLayout: ConstraintLayout
    private lateinit var mTabPublic: View
    private lateinit var mTabPrivate: View
    private lateinit var mTabIndex: View

    private lateinit var mInitSet: ConstraintSet

    private var currentTab: String = ""

    override fun initView(savedInstanceState: Bundle?) {
        mTabLayout = findViewById(R.id.m_tab_layout)
        mTabPublic = findViewById(R.id.m_tab_public)
        mTabPrivate = findViewById(R.id.m_tab_private)
        mTabIndex = findViewById(R.id.m_tab_index)
        mInitSet = ConstraintSet()
        mInitSet.clone(mTabLayout)
    }


    override fun initListener() {
        val click: (v: View) -> Unit = { onTabChanged(it) }
        mTabPublic.setOnClickListener(click)
        mTabPrivate.setOnClickListener(click)
        click.invoke(mTabPublic)

        findViewById<View>(R.id.m_local_theme).setOnClickListener {
            startLocalTheme()
        }
    }


    private fun newConstrainSet(itemView: View) = ConstraintSet().apply {
        val itemId = itemView.id
        clone(mInitSet)
        connect(R.id.m_tab_index, ConstraintSet.START, itemId, ConstraintSet.START)
        connect(R.id.m_tab_index, ConstraintSet.END, itemId, ConstraintSet.END)
    }


    private fun onTabChanged(itemView: View) {
        newConstrainSet(itemView).let {
            TransitionManager.beginDelayedTransition(mTabLayout)
            it.applyTo(mTabLayout)
        }
        val tabId = if (itemView.id == R.id.m_tab_public) TAB_PUBLIC else TAB_PRIVATE
        switchTab(currentTab, tabId)
        currentTab = tabId
    }


    private fun switchTab(currentTab: String, tabId: String) {
        supportFragmentManager.let { manager ->
            manager.beginTransaction().apply {
                manager.findFragmentByTag(currentTab)?.let {
                    hide(it)
                }
                manager.findFragmentByTag(tabId)?.let {
                    show(it)
                } ?: fragments.let {
                    var any: Pair<String, BaseFragment>? = null;
                    for (pair in it) {
                        if (TextUtils.equals(pair.first, tabId)) {
                            any = pair
                        }
                    }
                    any
                }?.let { it ->
                    add(R.id.m_frame_layout, it.second, it.first).show(it.second)
                }
            }.commitAllowingStateLoss()
        }
    }


}